package com.yourwork.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yourwork.model.Clientele;
import com.yourwork.model.Spending;
import com.yourwork.service.SpendingService;

@Controller
@RequestMapping(value="/spending")
public class SpendingController {
	@Autowired
	@Qualifier(value="expenseService")
	private SpendingService expenseService;
	/**
	 *查询费用
	 *@param page 页数
	 *@param size 每页显示的条数
	 **/
	@RequestMapping(value="/select",method={RequestMethod.GET})
	private String queryExpense(Model m,@RequestParam(name="page",required=false,defaultValue="0")int page,
			@RequestParam(name="size",required=false,defaultValue="10")int size,HttpSession session){
		try{
			Page<Spending> customerPage = expenseService.queryExpense(page,size);
			List<Spending> list = new ArrayList<Spending>();
			for(Spending s: customerPage){
				list.add(s);
			}
		double sumMoney = expenseService.querySumMoney();
		int count = expenseService.countExpense();
		addAttribute(m, "list", list, true);
		int maxPage = expenseService.queryMaxPage(page,size);
		m.addAttribute("maxPage", maxPage);
		m.addAttribute("sumMoney", sumMoney);
		m.addAttribute("page", page);//attribute中存入page
		m.addAttribute("size", size);//attribute中存入size
		m.addAttribute("count", count);//用户的总数
		m.addAttribute("remind", session.getAttribute("remind"));//获取session中的提示信息
		m.addAttribute("managerNumber", session.getAttribute("managerNumber"));
		session.setAttribute("remind", "");//清空session中的remind的数据
		return "/work/totalExpenses";}catch (Exception e) {
			// TODO: handle exception
			return "/work/totalExpenses";
		}
	}
	
	@RequestMapping(value="/addSpending",method={RequestMethod.POST})
	public String insertExpense(@ModelAttribute(name="expense") Spending expense,
			HttpSession session){
		String remind = expenseService.insertExpense(expense);
		session.setAttribute("remind", remind);
		return "redirect:/spending/select";
	}
	private void addAttribute(Model m,String attributeName,Object attributeValue,boolean replace){
		if(replace==true){//判断是否添加attribute
			m.addAttribute(attributeName, attributeValue);
		}else{
			if(!m.containsAttribute(attributeName)){//判断是否已经存在的attribute
				m.addAttribute(attributeName, attributeValue);
			}
		}
	}
}
